#include "general.h" 
#include "mainframe.h"

BEGIN_EVENT_TABLE(CMainFrame, wxFrame)
	EVT_CATEGORY_CHANGED(1, CMainFrame::OnCategoryChanged)
END_EVENT_TABLE()

CMainFrame::CMainFrame(void)
{
}

CMainFrame::CMainFrame( 
	wxWindow *parent, wxWindowID id, const wxString &title, 
	const wxPoint &position, const wxSize& size, long style 
	)
    : wxFrame( parent, id, title, position, size, style)
{
	InitMenu();

	m_pHeader = new CHeader(this);
	m_pFooter = new wxPanel(this);
	m_pControlBar = new CControlBar(this);
	m_pCanvas = new CCanvas(this);

	m_pSizer1 = new wxBoxSizer(wxVERTICAL);
	m_pSizer2 = new wxBoxSizer(wxHORIZONTAL);

	m_pSizer1->Add(m_pHeader, 0, wxEXPAND);
	m_pSizer1->AddSpacer(1);
	m_pSizer1->Add(m_pSizer2, 1, wxEXPAND);
	m_pSizer1->AddSpacer(1);
	m_pSizer1->Add(m_pFooter, 0, wxEXPAND);

	m_pSizer2->Add(m_pControlBar, 0, wxEXPAND);
	m_pSizer2->AddSpacer(1);
	m_pSizer2->Add(m_pCanvas, 1, wxEXPAND);

	m_pSizer1->SetItemMinSize(m_pFooter, 0, 50);

	m_pSizer2->SetItemMinSize(m_pControlBar, 300, 40);

	SetSizer(m_pSizer1);
	SetBackgroundColour(wxColor(255,255,255));
}

CMainFrame::~CMainFrame(void)
{
}

void CMainFrame::InitMenu()
{
	wxMenu* pMenuFile = new wxMenu();
	pMenuFile->Append(100, _("open..."));
	pMenuFile->Append(101, _("exit"));

	wxMenuBar* pMenuBar = new wxMenuBar();
	pMenuBar->Append(pMenuFile, _("File"));

	SetMenuBar(pMenuBar);

	Connect(100, wxEVT_COMMAND_MENU_SELECTED, 
		wxCommandEventHandler(CMainFrame::OnOpen)
		);
}


void CMainFrame::OnOpen(wxCommandEvent& event)
{
	wxFileDialog* pDialog = new wxFileDialog(this);
	if (pDialog->ShowModal() == wxID_OK)
	{
		wxImage image;
		if (image.LoadFile(pDialog->GetPath()))
		{
			m_pCanvas->SetImage(image);
		}
	}

}

#ifdef WIN32
WXLRESULT CMainFrame::MSWWindowProc ( WXUINT message, WXWPARAM wParam, WXLPARAM lParam )
{
        switch ( message )
        {
		case WM_SYSCOMMAND:
			break;
        }
        return wxFrame::MSWWindowProc ( message, wParam, lParam );
}
#endif

void CMainFrame::OnCategoryChanged(wxCommandEvent& event)
{
	wxString str;
	str << event.GetInt();
	wxMessageBox(str);
}